﻿using System;
using System.Transactions;
using KillBill.Business.Entities;
using KillBill.Business.Modules.Products;
using KillBill.Data.DataAccess;
using KillBill.Services.DTO;

namespace KillBill.Business.Modules.Sales
{

    public class SalesManager
    {
        private readonly KillBillEntities _context;
        private readonly ProductsManager _products;
        private readonly ProductOrders _orders;

        public SalesManager()
        {
            _context = new KillBillEntities();
            _products = new ProductsManager();
            _orders = new ProductOrders();
        }

        #region Sales Operation

        public SalesHeader RegisterSale(SalesHeader salesHeader)
        {
            try
            {
                using (var ts = new TransactionScope())
                {
                    salesHeader.ModifiedDate = DateTime.Now;
                    salesHeader.SaleDatetime = salesHeader.ModifiedDate;
                    _context.SalesHeader.AddObject(salesHeader);
                    #region Reduce Sold Products in Inventory

                    foreach (var saleDetail in salesHeader.SalesDetail)
                    {
                        ReduceSoldProductInventory(saleDetail);
                        saleDetail.ModifiedDate = salesHeader.ModifiedDate;
                    }
                    #endregion
                    _context.SaveChanges();
                    salesHeader.Messages.Add("La venta ha sido registrada con éxito");
                    salesHeader.Success = true;
                    ts.Complete();
                    salesHeader.SalesDetail.Clear();
                    return salesHeader;
                }
            }
            catch (Exception ex)
            {
                salesHeader.Success = false;
                salesHeader.Messages.Add("Ocurrió un error al registrar la venta.");
                salesHeader.Messages.Add(ex.Message);
                return salesHeader;
            }
        }
        public KillBillDTO ReduceSoldProductInventory(SalesDetail salesDetail)
        {
            var outDto = new KillBillDTO();
            var soldProduct = _products.FindProductById(salesDetail.ProductID);
            if (soldProduct.StockMax > salesDetail.OrderQty)
            {
                soldProduct.StockMax = soldProduct.StockMax - salesDetail.OrderQty;
                if(soldProduct.StockMax < soldProduct.StockMin)
                {
                    _orders.SendMailPurchaseOrder(soldProduct);
                }
                _products.UpdateProduct(soldProduct);
                outDto.Success = true;
            }
            else
            {
                outDto.Success = false;
                outDto.Messages.Clear();
                outDto.Messages.Add("No existe cantidad suficiente de "+ soldProduct.Name + "Para realizar la venta");
            }
            return outDto;
        }
        #endregion


    }
}
